package com.jms.product.web;

import com.jms.common.exception.BizCodeEnum;
import com.jms.common.utils.R;
import com.jms.product.constant.AuthServerConstant;
import com.jms.product.constant.RegisterSourceEnum;
import com.jms.product.entity.UserAuthEntity;
import com.jms.product.entity.UserBaseEntity;
import com.jms.product.feign.ThirdPartyFeignService;
import com.jms.product.service.RegisterService;
import com.jms.product.service.UserAuthService;
import com.jms.product.service.UserBaseService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Jamison
 * @version 1.0
 * @date 2021/6/11 14:46
 */
@RestController
public class RegisterController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserBaseService userBaseService;

    @Autowired
    private RegisterService registerService;

    /**
     * 手机号注册
     *
     * @param mobile
     * @param code
     * @return
     */
    @PostMapping("/user/register")
    public R register(@RequestParam("mobile") String mobile, @RequestParam("code") String code) {
        // 验证验证码是否正确
        String phonePre = AuthServerConstant.SMS_CODE_CACHE_PREFIX + mobile;
        String redisCode = redisTemplate.opsForValue().get(phonePre);
        if (redisCode == null || !redisCode.split("_")[0].equals(code)) {
            // 验证码不存在或者不正确，返回提示用户重新发送验证码
            return R.error(BizCodeEnum.CODE_OVERDUE_OR_ERROR_EXCEPTION.getCode(), BizCodeEnum.CODE_OVERDUE_OR_ERROR_EXCEPTION.getMsg());
        }

        UserBaseEntity entity = registerService.saveBaseUser(mobile);

        registerService.initUserAuth(mobile, entity);

        return R.ok();
    }


}
